// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Interop;
using System.Windows.Threading;
using Microsoft.WindowsAPICodePack.Taskbar;
using Microsoft.WindowsAPICodePack.Shell;

namespace TaskbarConcepts
{
    /// <summary>
    /// Interaction logic for ToolbarButtons.xaml
    /// </summary>
    public partial class ToolbarButtons : UserControl
    {
        public enum ButtonTask
        {
            First,
            Last,
            Next,
            Previous,
            All
        }

        public ToolbarButtons()
        {
            InitializeComponent();
            Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(
                () => this.Loaded += ToolbarButtons_Loaded));

            this.Unloaded += ToolbarButtons_Unloaded;
        }

        private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UpdateButtons();
        }

        private void ToolbarButtons_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.FilmStrip.Count == 0)
            {
                ErrorNoImages.Visibility = Visibility.Visible;
            }
            else
            {
                ErrorNoImages.Visibility = Visibility.Collapsed;
                listBox.SelectionChanged += new SelectionChangedEventHandler(listBox_SelectionChanged);
                this.listBox.ItemsSource = this.FilmStrip;

                ShowList(0);
            }

            if (buttonPrevious == null)
            {
                CreateToolbarButtons();
            }
            else
            {
                UpdateButtons();
            }
        }

        private void UpdateButtons()
        {
            ChangeVisibility(buttonPrevious, true, ButtonTask.Previous);
            ChangeVisibility(buttonFirst, true, ButtonTask.First);
            ChangeVisibility(buttonLast, true, ButtonTask.Last);
            ChangeVisibility(buttonNext, true, ButtonTask.Next);
        }

        private void ChangeVisibility(ThumbnailToolbarButton btn, bool show, ButtonTask task)
        {
            if (btn != null)
            {
                btn.Visible = show;
                btn.IsInteractive = show;

                switch (task)
                {
                    case ButtonTask.First:
                        btn.Enabled = listBox.SelectedIndex != 0;
                        break;

                    case ButtonTask.Last:
                        btn.Enabled = listBox.SelectedIndex != listBox.Items.Count - 1;
                        break;

                    case ButtonTask.Next:
                        btn.Enabled = listBox.SelectedIndex < listBox.Items.Count - 1;
                        break;

                    case ButtonTask.Previous:
                        btn.Enabled = listBox.SelectedIndex > 0;
                        break;
                }
            }

        }

        private void ChangeVisibility(ThumbnailToolbarButton btn, bool show)
        {
            ChangeVisibility(btn, show, ButtonTask.All);
        }

        private void ToolbarButtons_Unloaded(object sender, RoutedEventArgs e)
        {
            ChangeVisibility(buttonPrevious, false);
            ChangeVisibility(buttonFirst, false);
            ChangeVisibility(buttonLast, false);
            ChangeVisibility(buttonNext, false);
        }

        private void ShowList(int newIndex)
        {
            if (newIndex < 0 || newIndex > this.listBox.Items.Count - 1)
                return;

            this.listBox.SelectedIndex = newIndex;
            listBox.Focus();

            if (listBox.SelectedItem != null)
                listBox.ScrollIntoView(listBox.SelectedItem);
        }

        private ThumbnailToolbarButton buttonPrevious;
        private ThumbnailToolbarButton buttonNext;
        private ThumbnailToolbarButton buttonFirst;
        private ThumbnailToolbarButton buttonLast;

        //The following method is part of the lab. Create the thumbnail toolbar buttons
        //for the 'First Image', 'Previous Image', 'Next Image' and 'Last Image' commands
        //and add them to the thumbnail toolbar.
        private void CreateToolbarButtons()
        {
            buttonFirst = new ThumbnailToolbarButton(TaskbarConcepts.Resources.first, "First Image");
            buttonFirst.Enabled = false;
            buttonFirst.Click += buttonFirst_Click;

            buttonPrevious = new ThumbnailToolbarButton(TaskbarConcepts.Resources.prevArrow, "Previous Image");
            buttonPrevious.Enabled = false;
            buttonPrevious.Click += buttonPrevious_Click;

            buttonNext = new ThumbnailToolbarButton(TaskbarConcepts.Resources.nextArrow, "Next Image");
            buttonNext.Click += buttonNext_Click;

            buttonLast = new ThumbnailToolbarButton(TaskbarConcepts.Resources.last, "Last Image");
            buttonLast.Click += buttonLast_Click;

            TaskbarManager.Instance.ThumbnailToolbars.AddButtons(
                new WindowInteropHelper(Application.Current.MainWindow).Handle,
                buttonFirst, buttonPrevious, buttonNext, buttonLast);
        }

        private void buttonPrevious_Click(object sender, EventArgs e)
        {
            ShowList(this.listBox.SelectedIndex - 1);
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            ShowList(this.listBox.SelectedIndex + 1);
        }

        private void buttonFirst_Click(object sender, EventArgs e)
        {
            ShowList(0);
        }

        private void buttonLast_Click(object sender, EventArgs e)
        {
            ShowList(this.listBox.Items.Count - 1);
        }

        private List<string> FilmStrip
        {
            get
            {
                using (ShellLibrary lib = ShellLibrary.Load(KnownFolders.PicturesLibrary, true))
                {
                    string[] extensions = { ".jpg", ".png", ".bmp" };
                    return (from file in System.IO.Directory.GetFiles(lib.DefaultSaveFolder)
                            where extensions.Any(e => System.IO.Path.GetExtension(file) == e)
                            select file).Take(10).ToList();
                }
            }
        }
    }
}